/*
 * @Author: 9ilk 3535851673@qq.com
 * @Date: 2025-01-19 14:55:37
 * @LastEditors: 9ilk 3535851673@qq.com
 * @LastEditTime: 2025-01-23 11:19:47
 * @FilePath: /zhuang/httpserver/HttpServer.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"HttpServer.hpp"
#include<string>
#include"Session.hpp"
#include<cstring>
using namespace LogModule;

void Login(HttpRequest& req ,HttpResponse& resp)
{
    //根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入登录模块" << req.Path() << ", " << req.Args() ;
    string req_args = req.Args();
    //1.解析参数格式，得到要的格式

    //2.访问数据库，验证对应的用户是否是合法的用户以及其他工作...
    // SessionManager manager;
    // session_id = m.CreateSession(xxx);
    //3.登录成功
    // resp.SetCode(302);
    // resp.SetHeader("Location","/");
    // string body = "<html><body><p>Login Success! </p></body></html>";
    string body = req.GetContent("wwwroot/success.html");
    resp.SetCode(200);
    resp.SetHeader("Content_Length",to_string(body.size()));
    resp.SetHeader("Content-Type","text/html");
    resp.SetHeader("Set-Cookie","username=zhangsan&passwd=11111");//写入浏览器Cookie
    resp.SetBody(body);
}

void Register(HttpRequest& req ,HttpResponse& resp)
{
    //根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入注册模块" << req.Path() << ", " << req.Args() ;
}

void Search(HttpRequest& req ,HttpResponse& resp)
{
    //根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入搜索模块" << req.Path() << ", " << req.Args() ;
}

void Test(HttpRequest& req ,HttpResponse& resp)
{
    //根据req，动态构建resp
    LOG(LogLevel::DEBUG) << "进入测试模块" << req.Path() << ", " << req.Args() ;
}

int main(int argc,char* argv[])
{
  if(argc != 2)
  {
     cout << "Usage: "<< argv[0] << "port" <<endl;
  }
  auto httpserver = make_unique<HttpServer>(stoi(argv[1]));
  //要让服务器具有登录功能
   httpserver->Register("/login",Login);
   httpserver->Register("/register",Register);
   httpserver->Register("/search",Search);
   httpserver->Register("/test",Test);

  httpserver->Start();

}